'''
@create_time: 2025/10/23 下午4:59
@Author: GeChao
@File: upload_to_cos.py
'''
# -*- coding=utf-8
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
from tools.log import Logger

logger = Logger().logger

SECRET_ID = 'AKIDNZVAajaATVGk23nJYXFgqNegRv96mBJB'
SECRET_KEY = 'cw0nJYWFhsmPlpc8NM46GLPL4XlyzkER'


def upload_to_cos(file_data, cos_file_name):
    region = 'ap-guangzhou'
    token = None
    config = CosConfig(Region=region, SecretId=SECRET_ID, SecretKey=SECRET_KEY, Token=token)
    client = CosS3Client(config)
    try:
        response = client.put_object(
            Bucket='ops-1253885479',
            Body=file_data,
            Key=cos_file_name,
            EnableMD5=False
        )
        if response:
            return 'https://ops-1253885479.cos.ap-guangzhou.myqcloud.com%s' % cos_file_name
    except Exception as e:
        logger.error(f'{e}{cos_file_name} upload to cos fail!!' % cos_file_name)
        return None


if __name__ == '__main__':
    upload_to_cos(r'D:\项目\qa\case_report\allure-report\data\attachments\c0e4ba3c40976d53.png',
                  '/auto_test/c0e4ba3c40976d53.png')
